package lc.oa.ui;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.listener.DialogUIItemListener;

import java.util.Calendar;

import lc.oa.R;
/**
  *
  * @Description:     请假
  * @Author:         lucifer
  * @CreateDate:     2020/8/7 16:14
  * @UpdateUser:
  * @UpdateDate:
  * @UpdateRemark:
  * @Version:        1.0
 */
public class LeaveActivity extends AppCompatActivity implements View.OnTouchListener {

    ImageView ivReturn,ivClose;
    private TextView titleShow,leaveTypeTxt,leaveStartDateTxt,leaveEndDateTxt;
    private LinearLayout leave_type,leave_start_date,leave_end_date;
    private int LeaveType=13;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave);
        initView();//初始化页面主键
        backToStack();//返回
        buildMethod();//构建方法
    }
  //构建方法
    private void buildMethod() {
        //请假类型
        leave_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] leaveTypes = new String[]{"事假", "病假", "调休","年假","产检","哺乳假","婚假","丧假","陪产假","产检假"};
                DialogUIUtils.showSingleChoose(LeaveActivity.this, "", LeaveType-1, leaveTypes, new DialogUIItemListener() {
                    @Override
                    public void onItemClick(CharSequence text, int position) {
                        leaveTypeTxt.setText(text);
                        LeaveType=position+1;
                        Toast.makeText(LeaveActivity.this, text+"----"+position+1, Toast.LENGTH_SHORT).show();
                    }
                }).show();
            }
        });
        //开始时间与结束
        leave_start_date.setOnTouchListener(this);
        leave_end_date.setOnTouchListener(this);
    }

   //初始化组件
    private void initView() {
        titleShow=findViewById(R.id.txtTitleDesc);
        leave_type=findViewById(R.id.leave_type);
        leaveTypeTxt=findViewById(R.id.leave_type_txt);
        leave_start_date=findViewById(R.id.leaveStartDate);
        leaveStartDateTxt=findViewById(R.id.leaveStartDateTxt);
        leave_end_date=findViewById(R.id.leaveEndDate);
        leaveEndDateTxt=findViewById(R.id.leaveEndDateTxt);
    }
    //返回
    private void backToStack() {
        titleShow.setText("请假申请");
        ivReturn=findViewById(R.id.iv_return_back);
        ivClose=findViewById(R.id.iv_close);
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View view = View.inflate(this, R.layout.date_time_dialog, null);
            final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
            final TimePicker timePicker = (android.widget.TimePicker) view.findViewById(R.id.time_picker);
            builder.setView(view);

            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis());
            datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);
            if(LeaveType==1||LeaveType==2||LeaveType==3) {
                timePicker.setIs24HourView(true);
                timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
                timePicker.setCurrentMinute(Calendar.MINUTE);
            }else{
                timePicker.setVisibility(timePicker.GONE);
            }

            String date=cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.DATE)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+Calendar.MINUTE;
            Log.e("时间",date);

            if (v.getId() == R.id.leaveStartDate) {   //开始时间

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e("取消","--------");
                    }
                }).setPositiveButton("确  定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        StringBuffer sb = new StringBuffer();
                        sb.append(String.format("%d-%02d-%02d",
                                datePicker.getYear(),
                                datePicker.getMonth() + 1,
                                datePicker.getDayOfMonth()));
                        if(LeaveType==1||LeaveType==2||LeaveType==3) {
                            sb.append("  ");
                            sb.append(timePicker.getCurrentHour())
                                    .append(":").append(timePicker.getCurrentMinute());
                        }
                        leaveStartDateTxt.setText(sb);
                        dialog.cancel();
                    }
                });

            } else if (v.getId() == R.id.leaveEndDate) {
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e("取消","--------");
                    }
                }).setPositiveButton("确  定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        StringBuffer sb = new StringBuffer();
                        sb.append(String.format("%d-%02d-%02d",
                                datePicker.getYear(),
                                datePicker.getMonth() + 1,
                                datePicker.getDayOfMonth()));
                        if(LeaveType==1||LeaveType==2||LeaveType==3) {
                            sb.append("  ");
                            sb.append(timePicker.getCurrentHour())
                                    .append(":").append(timePicker.getCurrentMinute());
                        }
                        leaveEndDateTxt.setText(sb);
                        dialog.cancel();
                    }
                });
            }

            AlertDialog dialog = builder.create();
            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialogInterface) {
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#1A90FF"));
                    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor("#1A90FF"));
                }
            });
            dialog.show();
//            setDialogWindowAttr(dialog,DateTimeActivity.this);
            //设置大小
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels*0.75), dialog.getWindow().getAttributes().height);

        }

        return true;
    }
}